@extends('admin.main')

@section('body')
    <!-- 标题栏 -->
    <div class="main-title">
        <h2>渠道码列表</h2>
    </div>

    <div class="cf">
        @if(plat_identify() == 'account')
            <div class="fl">
                <a class="btn" href="javascript:;" onclick="createCode()">生成渠道码</a>
            </div>
        @endif
        <div class="search-form fr cf">
            <div class="sleft">
                <input type="text" id="keyword" name="keyword" class="search-input" value="{{request('keyword')}}"
                       placeholder="请输入渠道码">
                <a class="sch-btn" href="javascript:;" id="search" url="{{urlPlat($ctr,'getRefer')}}"><i
                            class="btn-search"></i></a>
            </div>
        </div>
    </div>
    <div class="data-table table-striped">
        <table class="">
            <thead>
            <tr>
                <th class="">序号</th>
                <th class="">渠道码</th>
                <th class="">店铺名称</th>
                <th class="">用户名</th>
                <th class="">状态</th>
                @if(plat_identify() == 'account')
                    <th class="">操作</th>
                @endif
            </tr>
            </thead>
            <tbody>
            @if(count($rows['data']))
                @foreach($rows['data'] as $v)
                    <tr>
                        <td>{{$v['id']}}</td>
                        <td>{{$v['code']}}</td>
                        <td>{{$v['shop_name']}}</td>
                        <td>{{$v['nickname']}}</td>
                        <td @if(!$v['status']) style="color: red"@endif>{{$v['status_text']}}</td>
                        @if(plat_identify() == 'account')
                            <td>
                                @if($v['code_status'] == 2)
                                    <a href="{{urlPlat($ctr, 'getReferSet', ['refer_id' => $v['id']])}}">绑定用户</a>
                                @endif
                                @if($v['code_status'] == 3)
                                    <a href="{{urlPlat($ctr,'getReferQr',['mid' => $v['mid']])}}" >渠道码下载</a>
                                @endif
                            </td>
                        @endif
                    </tr>
                @endforeach
            @else
                <td colspan="12" class="text-center"> aOh! 暂时还没有内容!</td>
            @endif
            </tbody>
        </table>
    </div>
    <div class="page">
        @if($rows['last_page']>1)
            <div>
                @if($rows['prev_page_url'])
                    <a class="prev" href="{{$rows['prev_page_url']}}">&lt;&lt;</a>
                @endif
                @if(count($rows['pageNoList']))
                    @foreach($rows['pageNoList'] as $v)
                        @if($rows['current_page'] == $v)
                            <span class="current">{{$v}}</span>
                        @else
                            <a class="num" href="{{urlPlat($ctr,'getRefer',['page'=>$v,'code_status'=>request('codeStatus',0)])}}">{{$v}}</a>
                        @endif
                    @endforeach
                @endif
                @if($rows['next_page_url'])
                    <a class="next" href="{{$rows['next_page_url']}}">&gt;&gt;</a>
                @endif
                <span class="rows">共 {{$rows['total']}} 条记录</span>
            </div>
        @endif
    </div>
@endsection

@section('script')
<script src="/static/layer/extend/layer.ext.js"></script>
<script type="text/javascript">

    //搜索功能
    $("#search").click(function () {
        var url = $(this).attr('url');
        var query = $('.search-form').find('input').serialize();
        var status = $('#code_status').val() || 0;
        query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g, '');
        query = query.replace(/^&/g, '');
        if (url.indexOf('?') > 0) {
            url += '&' + query + '&code_status=' + status;
        } else {
            url += '?' + query + '&code_status=' + status;
        }
        window.location.href = url;
    });

    function createCode() {
        var shop_id = "{{$shop_id}}";
        layer.prompt({
            formType:0,
            title:'请输入数量',
        },function(val, index){
            if(val != '' && val != 0) {
                $.ajax({
                    url:"{{urlPlat($ctr,'postRefer')}}",
                    data:{num:val, shop_id:shop_id},
                    type:'post',
                    success:function(res){
                        if(res.status == 1){
                            layer.msg('生成渠道码成功');
                            window.location.reload()
                        } else {
                            layer.msg(res.msg)
                        }
                    },
                    fail:function(){
                        layer.msg('生成渠道码失败')
                    }
                })
            } else {
                layer.msg('请输入正整数')
            }
            layer.close(index);
        });
    }
</script>
@endsection